package pl.jtb.ctl.tabory.ui.boe.ui.se;

import java.util.LinkedHashMap;
import java.util.Map;

import pl.jtb.ctl.tabory.dao.data.vo.StationVO;
import pl.jtb.ctl.tabory.ui.boe.selection.StationSelection;
import pl.jtb.ctl.tabory.ui.selection.ISelectionListener;
import pl.jtb.ctl.tabory.ui.selection.ISelectionProvider;
import de.kupzog.ktable.KTableCellEditor;
import de.kupzog.ktable.KTableCellRenderer;
import de.kupzog.ktable.KTableDefaultModel;
import de.kupzog.ktable.editors.KTableCellEditorText;

public class SampleModel extends KTableDefaultModel implements ISelectionProvider<StationSelection> {

    private StationSelection selection;
    private ISelectionListener<StationSelection> listener;

    public SampleModel(StationSelection selection) {
        initialize();
        this.selection = selection;
    }

    @Override
    public int getFixedHeaderColumnCount() {
        return 1;
    }

    @Override
    public int getFixedHeaderRowCount() {
        return 1;
    }

    @Override
    public int getFixedSelectableColumnCount() {
        return 1;
    }

    @Override
    public int getFixedSelectableRowCount() {
        return 1;
    }

    @Override
    public int getRowHeightMinimum() {
        return 1;
    }

    @Override
    public boolean isColumnResizable(int arg0) {
        return true;
    }

    @Override
    public boolean isRowResizable(int arg0) {
        return true;
    }

    @Override
    public KTableCellEditor doGetCellEditor(int row, int col) {
        if (row == col) {
            return null;
        }
        if (row > 0 && col > 0) {
            return new KTableCellEditorText();
        }
        return null;
    }

    @Override
    public KTableCellRenderer doGetCellRenderer(int row, int col) {
        return KTableCellRenderer.defaultRenderer;
    }

    @Override
    public int doGetColumnCount() {
        return selection.getListStations().size() + 1;
    }

    @Override
    public Object doGetContentAt(int row, int col) {
        if (row == 0 && col == 0) {
            return "";
        }
        if (row == 0 && col > 0) {
            return selection.getListStations().get(col - 1).getStationName();
        }
        if (col == 0 && row > 0) {
            return selection.getListStations().get(row - 1).getStationName();
        }
        if (row == col) {
            return "0.0";
        }
        StationVO stationOnRow = selection.getListStations().get(row - 1);
        StationVO stationOnCol = selection.getListStations().get(col - 1);
        if (stationOnCol != null && stationOnRow != null) {
            Double value = selection.getMapDistances().get(stationOnRow).get(stationOnCol);
            if (value != null) {
                return value;
            } else {
                return "---";
            }
        }
        return "---";
    }

    @Override
    public int doGetRowCount() {
        return selection.getListStations().size() + 1;
    }

    @Override
    public void doSetContentAt(int row, int col, Object value) {
        StationVO stationOnRow = selection.getListStations().get(row - 1);
        StationVO stationOnCol = selection.getListStations().get(col - 1);
        Map<StationVO, Double> map = selection.getMapDistances().get(stationOnRow);
        if (map == null) {
            map = new LinkedHashMap<StationVO, Double>();
        }
        Double doubleValue = Double.parseDouble((String) value);
        map.put(stationOnCol, doubleValue);
        selection.getMapDistances().put(stationOnRow, map);
        map = selection.getMapDistances().get(stationOnCol);
        if (map == null) {
            map = new LinkedHashMap<StationVO, Double>();
        }
        doubleValue = Double.parseDouble((String) value);
        map.put(stationOnRow, doubleValue);
        selection.getMapDistances().put(stationOnCol, map);
        listener.selectionChanged(getSelection());
    }

    @Override
    public int getInitialColumnWidth(int arg0) {
        return 60;
    }

    @Override
    public int getInitialRowHeight(int arg0) {
        return 20;
    }

    @Override
    public StationSelection getSelection() {
        return null;
    }

    @Override
    public void setSelection(StationSelection selection) {
        this.selection = selection;
    }

    @Override
    public void addSelectionListener(ISelectionListener<StationSelection> listner) {
        this.listener = listner;
    }

    @Override
    public void removeSelectionListener(ISelectionListener<StationSelection> listner) {
        this.listener = null;
    }

}
